package org.example;

import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
//import org.geotools.streamparser.shp.ShapefileDataStore;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.scene.image.ImageView;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;


public class ShapefileHandler{
    private Stage primaryStage;
    private ImageView imageView;
    private File selectedFile;
    private ObservableList<String> importedFiles = FXCollections.observableArrayList();
    private List<String> importedLayers = new ArrayList<>();

    public ShapefileHandler(Stage primaryStage, ImageView imageView) {
        this.primaryStage = primaryStage;
        this.imageView = imageView;
    }
    public ObservableList<String> getImportedFiles() {
        return importedFiles;
    }

    public List<String> getImportedLayers() {
        return importedLayers;
    }
    public void handleOpenShapefile(){
        FileChooser fileChooser = new FileChooser();
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Shapefile", "*.shp"));
        selectedFile = fileChooser.showOpenDialog(primaryStage);
        if (selectedFile!= null) {
//            try {
//
//            } catch (IOException e) {
//
//            }
        }
    }




    // 添加获取文件名的方法
    public String getFileName() {
        if (selectedFile!= null) {
            return selectedFile.getName();
        }
        return null;
    }
    public void handleCloseShapefile(){
        Platform.runLater(() -> {
            imageView.setImage(null);
        });
    }
    private void showSuccessAlert(String message) {
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("成功");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }

    private void showErrorAlert(String message) {
        Alert alert = new Alert(AlertType.ERROR);
        alert.setTitle("错误");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }
}
